/**
 *
 * @author DorcasBless
 * you can find the Synaxarium at http://code.google.com/p/synaxarium/
 * April, 2009
 * Version 1.0.0
 *
 * some code snippets were inspired from GoBible http://code.google.com/p/gobible
 *
 */

package com.christian;

import javax.microedition.lcdui.*;

public class PrefsForm extends Form implements CommandListener, ItemCommandListener {
    //private GoBible goBible;

    private SynMIDlet synMIDlet;
    private ChoiceGroup fontSizeChoice;
    private ChoiceGroup fontStyleChoice;
    private ChoiceGroup fullScreenChoice;
    //private ChoiceGroup dateTypeChoice;
    private ChoiceGroup reverseColoursChoice;
    private ColourItem[] themeItems = new ColourItem[]{
        new ColourItem(0, "\u0648\u0631\u0642\u064A\u0629", Constatnts.THEME_TEXT_COLOUR[0], Constatnts.THEME_BACK_COLOUR[0]),
        new ColourItem(1, "\u0643\u0645\u0628\u064A\u0648\u062A\u0631", Constatnts.THEME_TEXT_COLOUR[1], Constatnts.THEME_BACK_COLOUR[1]),
        new ColourItem(2, "\u0632\u0647\u0631\u064A\u0629", Constatnts.THEME_TEXT_COLOUR[2], Constatnts.THEME_BACK_COLOUR[2]),
        new ColourItem(3, "\u0637\u0628\u064A\u0639\u0629", Constatnts.THEME_TEXT_COLOUR[3], Constatnts.THEME_BACK_COLOUR[3]),
        new ColourItem(4, "\u0632\u0631\u0642\u0627\u0621", Constatnts.THEME_TEXT_COLOUR[4], Constatnts.THEME_BACK_COLOUR[4]),
        new ColourItem(5, "\u0635\u0641\u0631\u0627\u0621", Constatnts.THEME_TEXT_COLOUR[5], Constatnts.THEME_BACK_COLOUR[5]),};
    private ColourItem themeItem;
    private Command saveCommand = new Command("\u062D\u0641\u0638", Command.OK, 0);
    private Command cancelCommand = new Command("\u0627\u0644\u063A\u0627\u0621", Command.CANCEL, 0);
    private Command selectCommand = new Command("\u0627\u062E\u062A\u064A\u0627\u0631", Command.ITEM, 0);

    // Theme Form
    private Form themeForm;

    /**
     * Setup the Preferences form.
     */
    public PrefsForm(SynMIDlet synMIDlet) {

        super("\u0627\u062E\u062A\u064A\u0627\u0631\u0627\u062A");

        this.synMIDlet = synMIDlet;

        append(new StringItem("\u0646\u0648\u0639 \u0627\u0644\u062E\u0644\u0641\u064A\u0629" + ":", null));

        ColourItem storedThemeItem = themeItems[synMIDlet.theme];

        themeItem = new ColourItem(storedThemeItem.id, storedThemeItem.text, storedThemeItem.textColour, storedThemeItem.backColour);

        themeItem.addCommand(new Command("\u062A\u063A\u064A\u064A\u0631", Command.ITEM, 0));

        themeItem.setItemCommandListener(this);

        append(themeItem);

        // Setup the commands and listeners for the themes form
        for (int i = 0; i < themeItems.length; i++) {
            themeItems[i].addCommand(selectCommand);
            themeItems[i].setItemCommandListener(this);
        }

        fontSizeChoice = new ChoiceGroup("\u062D\u062C\u0645 \u0627\u0644\u062E\u0637" + ":", Choice.EXCLUSIVE, new String[]{"\u0635\u063A\u064A\u0631", "\u0645\u062A\u0648\u0633\u0637", "\u0643\u0628\u064A\u0631"}, null);
        fontSizeChoice.setFont(0, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_SMALL));
        fontSizeChoice.setFont(1, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        fontSizeChoice.setFont(2, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_LARGE));
        fontSizeChoice.setSelectedIndex(synMIDlet.fontSize, true);
        append(fontSizeChoice);

        fontStyleChoice = new ChoiceGroup("\u0646\u0645\u0637 \u0627\u0644\u062E\u0637" + ":", Choice.EXCLUSIVE, new String[]{"\u0639\u0627\u062F\u0649", "\u0639\u0631\u064A\u0636"}, null);
        fontStyleChoice.setFont(0, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM));
        fontStyleChoice.setFont(1, Font.getFont(Font.FACE_SYSTEM, Font.STYLE_BOLD, Font.SIZE_MEDIUM));
        fontStyleChoice.setSelectedIndex(synMIDlet.fontStyle, true);
        append(fontStyleChoice);

        if (Constatnts.MIDP20) {
            fullScreenChoice = new ChoiceGroup("\u0634\u0627\u0634\u0629 \u0643\u0627\u0645\u0644\u0629" + ":", Choice.EXCLUSIVE, new String[]{"\u0645\u0646\u0634\u0637", "\u063A\u064A\u0631 \u0645\u0646\u0634\u0637"}, null);
            fullScreenChoice.setSelectedIndex(synMIDlet.fullScreen ? 0 : 1, true);
            append(fullScreenChoice);
        }

        reverseColoursChoice = new ChoiceGroup("\u0639\u0643\u0633 \u0627\u0644\u0623\u0644\u0648\u0627\u0646" + ":", Choice.EXCLUSIVE, new String[]{"\u0645\u0646\u0634\u0637", "\u063A\u064A\u0631 \u0645\u0646\u0634\u0637"}, null);
        reverseColoursChoice.setSelectedIndex(synMIDlet.reverseColours ? 0 : 1, true);
        append(reverseColoursChoice);

//        dateTypeChoice = new ChoiceGroup("\u0646\u0648\u0639 \u0627\u0644\u062A\u0627\u0631\u064A\u062E \u0627\u0644\u0645\u0639\u0631\u0648\u0636" + ":", Choice.EXCLUSIVE, new String[]{"\u0642\u0628\u0637\u0649", "\u0645\u064A\u0644\u0627\u062F\u0649"}, null);
//        dateTypeChoice.setSelectedIndex(synMIDlet.dateType ? 0 : 1, true);
//        append(dateTypeChoice);
        
        addCommand(saveCommand);
        addCommand(cancelCommand);
        setCommandListener(this);
    }

    /**
     * Part of CommandListener.
     */
    public void commandAction(Command command, Displayable display) {
        // If the prefs form is being displayed then process accordingly
        // otherwise the commands are coming from the themes form
        if (display == this) {
            switch (command.getCommandType()) {
                case Command.OK: {
                    // Save has been pressed so save the prefs
                    synMIDlet.fontSize = fontSizeChoice.getSelectedIndex();
                    synMIDlet.fontStyle = fontStyleChoice.getSelectedIndex();

                    if (Constatnts.MIDP20) {
                        synMIDlet.fullScreen = (fullScreenChoice.getSelectedIndex() == 0);
                    }

                    synMIDlet.theme = themeItem.id;
                    synMIDlet.reverseColours = (reverseColoursChoice.getSelectedIndex() == 0);
                    //synMIDlet.dateType = (dateTypeChoice.getSelectedIndex() == 0);

                    // Go back to the main screen
                    synMIDlet.showSameMainScreen();
                    break;
                }
                // Go back to the main screen without saving
                case Command.BACK:
                case Command.CANCEL: {
                    //synMIDlet.showSameMainScreen();
                    synMIDlet.showSameMainScreen();
                }
            }
        } else if (display == themeForm) {
            // The command is coming from the themes form
            switch (command.getCommandType()) {
                // Go back to the prefs screen without changing the theme
                case Command.BACK:
                case Command.CANCEL: {
                    SynMIDlet.display.setCurrent(this);
                }
            }

        }
    }

    /**
     * Called when the user selects the theme to change it or when
     * a new theme has been selected in the theme form.
     */
    public void commandAction(Command command, Item item) {
        if (item == themeItem) {
            displayThemeForm();
        } else if (item instanceof ColourItem) {
            // A new theme has been selected so update the themeItem
            ColourItem newThemeItem = (ColourItem) item;
            themeItem.id = newThemeItem.id;
            themeItem.setText(newThemeItem.text);
            themeItem.textColour = newThemeItem.textColour;
            themeItem.backColour = newThemeItem.backColour;
            themeItem.notifyStateChanged();

            // Display the prefs form
            SynMIDlet.display.setCurrent(this);
        }
    }

    /**
     * Display a list of themes for the user to select from.
     */
    private void displayThemeForm() {
        if (themeForm == null) {
            themeForm = new Form("\u062E\u0644\u0641\u064A\u0627\u062A", themeItems);
            themeForm.addCommand(cancelCommand);
            themeForm.setCommandListener(this);
        }

        // Display the theme form
        SynMIDlet.display.setCurrent(themeForm);
    }
}